package com.juttec.freshbird.pet.fragment;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.juttec.freshbird.pet.MainActivity;
import com.juttec.freshbird.pet.R;
import com.juttec.freshbird.pet.base.BasePager;
import com.juttec.freshbird.pet.base.baseimpl.ZixunPager;
import com.juttec.freshbird.pet.base.baseimpl.ShangchengPager;
import com.juttec.freshbird.pet.base.baseimpl.WoPager;
import com.juttec.freshbird.pet.base.baseimpl.LuntanPager;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

/**
 * Created by freshbird on 2015/10/19.
 */
public class ContentFragment extends BaseFragment {

//    @ViewInject(R.id.vp_content)
    private ViewPager mViewPager;
//    @ViewInject(R.id.rg_group)
    private RadioGroup rgGroup;
    public ArrayList<BasePager> mPagerList;

    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.content_fragment, null);
        rgGroup = (RadioGroup) view.findViewById(R.id.rg_group);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_content);
        ViewUtils.inject(this, view);
        initCebianlan();
        return view;
    }

    private void initCebianlan() {
        MainActivity mainUi = (MainActivity) mActivity;
        LeftFragment leftMenuFragment = mainUi.getLeftMenuFragment();
        leftMenuFragment.setLeftAdapter();
    }

    public void initData() {
        //设置选择资讯按钮，即最先显示资讯按钮的界面
        rgGroup.check(R.id.rb_zixun);
        //新建basepager的arraylist
        mPagerList = new ArrayList<BasePager>();
        //将四个界面新建的实例添加到列表中
        mPagerList.add(new ZixunPager(mActivity));
        mPagerList.add(new LuntanPager(mActivity));
        mPagerList.add(new ShangchengPager(mActivity));
        mPagerList.add(new WoPager(mActivity));

        //这个viewpager是辈分最大的viewpager
        //因为其余viewpager都是在这个viewpager的基础上加载的
        mViewPager.setAdapter(new ContentAdapter());
        rgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_zixun:
                        mViewPager.setCurrentItem(0, false);
                        initCebianlan();
                        break;
                    case R.id.rb_luntan:
                        mViewPager.setCurrentItem(1, false);
                        break;
                    case R.id.rb_shangcheng:
                        mViewPager.setCurrentItem(2, false);
                        break;
                    case R.id.rb_wo:
                        mViewPager.setCurrentItem(3, false);
                        break;
                    default:
                        break;
                }

            }
        });
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mPagerList.get(position).initData();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
            mPagerList.get(0).initData();
    }

    class ContentAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mPagerList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        //初始化界面
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager pager = mPagerList.get(position);
            container.addView(pager.mRootView);
            return pager.mRootView;

        }

        //销毁界面
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
